package com.ugoodtech.mdcc.core.utils;


import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Kotone
 * Date: 2017/9/21
 * Time: 14:47
 * To change this template use File | Settings | File Templates.
 */
public class ConvertUtil {


    public  static Object convertToClientDTO(Object data, Map<String,Object> source)  {
        String name=data.getClass().getSimpleName();
        String pkg=data.getClass().getPackage().getName();
        String[] pkgArray=pkg.split("\\.");
        pkgArray[pkgArray.length-1]="dto";
        pkgArray[pkgArray.length-2]="client";
        String dtoPkg= StringUtils.join(pkgArray,".");
        String dtoName=dtoPkg+"."+name+"Response";
        try {
            Class dtoClass = Class.forName(dtoName);
            BaseDto baseDto=(BaseDto)dtoClass.newInstance();
            baseDto.setOriginData(data);
            baseDto.setSource(source);
            return baseDto;
        } catch (ClassNotFoundException e) {
            return data;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return data;
        } catch (InstantiationException e) {
            e.printStackTrace();
            return data;
        }
    }

    public  static Object convertToClientDTO(Object data)  {
       return convertToClientDTO(data,null);
    }

    public  static Object convertToWebDTO(Object data, Map<String,Object> source)  {
        String name=data.getClass().getSimpleName();
        String pkg=data.getClass().getPackage().getName();
        String[] pkgArray=pkg.split("\\.");
        pkgArray[pkgArray.length-1]="dto";
        pkgArray[pkgArray.length-2]="web";
        String dtoPkg= StringUtils.join(pkgArray,".");
        String dtoName=dtoPkg+"."+name+"Response";
        try {
            Class dtoClass = Class.forName(dtoName);
            BaseDto baseDto=(BaseDto)dtoClass.newInstance();
            baseDto.setOriginData(data);
            baseDto.setSource(source);
            return baseDto;
        } catch (ClassNotFoundException e) {
            return data;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return data;
        } catch (InstantiationException e) {
            e.printStackTrace();
            return data;
        }
    }

    public  static Object convertToManagerDTO(Object data, Map<String,Object> source)  {
        String name=data.getClass().getSimpleName();
        String pkg=data.getClass().getPackage().getName();
        String[] pkgArray=pkg.split("\\.");
        pkgArray[pkgArray.length-1]="dto";
        pkgArray[pkgArray.length-2]="manager";
        String dtoPkg= StringUtils.join(pkgArray,".");
        String dtoName=dtoPkg+"."+name+"Response";
        try {
            Class dtoClass = Class.forName(dtoName);
            BaseDto baseDto=(BaseDto)dtoClass.newInstance();
            baseDto.setOriginData(data);
            baseDto.setSource(source);
            return baseDto;
        } catch (ClassNotFoundException e) {
            return data;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return data;
        } catch (InstantiationException e) {
            e.printStackTrace();
            return data;
        }
    }

    public  static Object convertToManagerDTO(Object data)  {
        return convertToManagerDTO(data,null);
    }

}
